package com.steveproject.parttimeworktree.ui.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.view.View;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.steveproject.parttimeworktree.App;
import com.steveproject.parttimeworktree.R;
import com.steveproject.parttimeworktree.adapter.PartApplyAdapter;
import com.steveproject.parttimeworktree.base.BaseActivity;
import com.steveproject.parttimeworktree.entity.DailyEntity;
import com.steveproject.parttimeworktree.entity.PartBaseEntity;
import com.steveproject.parttimeworktree.net.MyCallback;
import com.steveproject.parttimeworktree.net.NoValidationTask;
import com.steveproject.parttimeworktree.utils.AppConfig;
import com.steveproject.parttimeworktree.utils.TitleBuilder;
import com.steveproject.putorefresh.BaseBothListener;
import com.steveproject.putorefresh.PullToBothLayout;
import com.steveproject.putorefresh.ViewStatus;
import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Response;

public class PartHomeCenterActivity extends BaseActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.pullRefresh)
    PullToBothLayout pullToBothLayout;

    @BindView(R.id.iv_1)
    RoundedImageView iv_1;
    private PartApplyAdapter dailyAdapter = null;
    private List<DailyEntity> list = new ArrayList<>();

    private int page = 1;
    private boolean isRefresh = true;
    private String type;
    private String flag;//传的参数类型 4 高新推荐  5 宝妈专场
    @Override
    public void setListen() {
        pullToBothLayout.setRefreshListener(new BaseBothListener() {
            @Override
            public void refresh() {
                page = 1;
                isRefresh = true;
                getJobList();
            }

            @Override
            public void loadMore() {
                page++;
                isRefresh = false;
                getJobList();
            }
        });
    }

    //关闭pullTo
    private void closePullTo() {
        pullToBothLayout.finishRefresh();
        pullToBothLayout.finishLoadMore();
    }

    @Override
    public void setTitleBarColor() {

    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_part_home_center;
    }

    @Override
    public void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        if (dailyAdapter == null) {
            dailyAdapter = new PartApplyAdapter(PartHomeCenterActivity.this, list);
        }
        recyclerView.setHasFixedSize(true);
        recyclerView.setNestedScrollingEnabled(false);
        // 第一种，直接取消动画
        RecyclerView.ItemAnimator animator = recyclerView.getItemAnimator();
        if (animator instanceof SimpleItemAnimator) {
            ((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
        }
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(dailyAdapter);
        dailyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //跳转兼职详情
                startActivity(new Intent(PartHomeCenterActivity.this, PartDetailActivity.class).putExtra("jobId", list.get(position).getJobId()));
            }
        });
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        type = intent.getStringExtra("type");
        assert type != null;
        switch (type) {
            case "0"://高新推荐
                flag="4";
                Picasso.with(this).load(AppConfig.getInstance(App.getContext()).get("img_home_card1_1")).into(iv_1);
                new TitleBuilder(this).setTitleText("高新推荐")
                        .setLeftIcoListening(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                finish();
                            }
                        });
                break;
            case "1"://宝妈专场
                flag="5";
                Picasso.with(this).load(AppConfig.getInstance(App.getContext()).get("img_home_card2_2")).into(iv_1);
                new TitleBuilder(this).setTitleText("宝妈专场")
                        .setLeftIcoListening(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                finish();
                            }
                        });
                break;
        }
        getJobList();

    }
    //获取job列表
    //获取列表信息
    private void getJobList() {
        if (isRefresh)
            pullToBothLayout.showView(ViewStatus.LOADING_STATUS);
        NoValidationTask.getApiService().getRM(flag, page).enqueue(new MyCallback<PartBaseEntity>(PartHomeCenterActivity.this) {
            @Override
            protected void onSuccess(Response<PartBaseEntity> response) {
                assert response.body() != null;
                String jsonStr = JSONObject.toJSONString(response.body().getData());
                JSONArray jsonArray = JSONArray.parseArray(jsonStr);
                if (isRefresh) {
                    list.clear();
                    if (jsonArray.size() > 0) {
                        pullToBothLayout.showView(ViewStatus.CONTENT_STATUS);
                        for (int i = 0; i < jsonArray.size(); i++) {
                            DailyEntity dailyEntity = new DailyEntity();
                            dailyEntity.setTitle(jsonArray.getJSONObject(i).getString("title"));
                            dailyEntity.setJobId(jsonArray.getJSONObject(i).getInteger("id") + "");
                            dailyEntity.setLabel(jsonArray.getJSONObject(i).getString("jobPayType"));
                            dailyEntity.setMoney(jsonArray.getJSONObject(i).getInteger("jobPay") + "");
                            dailyEntity.setUnit(jsonArray.getJSONObject(i).getString("jobPayUnit"));
                            dailyEntity.setGsName(jsonArray.getJSONObject(i).getString("jobCompany"));
                            dailyEntity.setLocation(jsonArray.getJSONObject(i).getString("jobAddress"));
                            list.add(dailyEntity);
                        }

                    } else {
                        pullToBothLayout.showView(ViewStatus.EMPTY_STATUS);
                    }
                } else {
                    pullToBothLayout.showView(ViewStatus.CONTENT_STATUS);
                    if (jsonArray.size() > 0) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            DailyEntity dailyEntity = new DailyEntity();
                            dailyEntity.setTitle(jsonArray.getJSONObject(i).getString("title"));
                            dailyEntity.setJobId(jsonArray.getJSONObject(i).getInteger("id") + "");
                            dailyEntity.setLabel(jsonArray.getJSONObject(i).getString("jobPayType"));
                            dailyEntity.setMoney(jsonArray.getJSONObject(i).getInteger("jobPay") + "");
                            dailyEntity.setUnit(jsonArray.getJSONObject(i).getString("jobPayUnit"));
                            dailyEntity.setGsName(jsonArray.getJSONObject(i).getString("jobCompany"));
                            dailyEntity.setLocation(jsonArray.getJSONObject(i).getString("jobAddress"));
                            list.add(dailyEntity);
                        }
                    }
                }
                dailyAdapter.notifyDataSetChanged();
                closePullTo();
            }

            @Override
            protected void onFailure(Throwable t) {

            }
        });
    }
}
